package cn.iocoder.yudao.module.cms.agreement.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cms.agreement.bo.AgreementIdentifyBO;
import cn.iocoder.yudao.module.cms.agreement.dal.dataobject.AgreementDO;
import cn.iocoder.yudao.module.cms.agreement.service.AgreementService;
import cn.iocoder.yudao.module.cms.agreement.vo.AgreementRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.api.cms.ErrorCodeConstants.AGREEMENT_NOT_EXISTS;

@Tag(name = "用户 App - 协议规则", description = "可以用于单个静态页面及弹窗页面，如用户服务协议、隐私政策、提现规则")
@RestController
@RequestMapping("/cms/agreement")
@Validated
public class AppAgreementController {

    @Resource
    private AgreementService agreementService;

    @GetMapping("/all-identify")
    @Operation(summary = "已定义的所有标识")
    @PermitAll
    public CommonResult<List<AgreementIdentifyBO>> getAllIdentify() {
        List<AgreementIdentifyBO> allIdentify = agreementService.getAllIdentify();
        return success(allIdentify);
    }

    @GetMapping("/get-by-identify")
    @Operation(summary = "获得协议规则内容")
    @Parameter(name = "identify", description = "标识，如：about_us-关于我们、service_agreement-服务协议、……", required = true, example = "about_us")
    @PermitAll
    public CommonResult<AgreementRespVO> getAgreement(@RequestParam("identify") String identify) {
        AgreementDO content = agreementService.getContent(identify);
        if (content == null) {
            return success(null);
        }
        return success(BeanUtils.toBean(content, AgreementRespVO.class));
    }


    @GetMapping("/get-content")
    @Operation(summary = "获得协议规则内容")
    @Parameter(name = "identify", description = "标识，如：about_us-关于我们、service_agreement-服务协议、……", required = true, example = "about_us")
    @PermitAll
    public CommonResult<String> getAgreementContent(@RequestParam("identify") String identify) {
        AgreementDO content = agreementService.getContent(identify);
        if (content == null) {
            return success(null);
        }
        return success(BeanUtils.toBean(content, AgreementRespVO.class).getContent());
    }

    @GetMapping(value = "/get-html", produces = "text/html;charset=UTF-8")
    @Operation(summary = "获得HTML页面", description = "可直接作为网页进行加载")
    @Parameter(name = "identify", description = "标识，如：about_us-关于我们、service_agreement-服务协议、……", required = true, example = "about_us")
    @ApiResponse(content = {@Content(mediaType = "text/html")})
    @PermitAll
    public String getAgreementHtml(@RequestParam("identify") String identify) {
        AgreementDO agreementDO = agreementService.getContent(identify);
        String title;
        String content;
        if (agreementDO == null) {
            title = AGREEMENT_NOT_EXISTS.getMsg();
            content = title;
        } else {
            AgreementRespVO respVO = BeanUtils.toBean(agreementDO, AgreementRespVO.class);
            title = respVO.getTitle();
            content = respVO.getContent();
        }
        return ("""
                <!DOCTYPE html>
                <html lang="zh">
                <head>
                    <meta charset="UTF-8" />
                    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
                    <meta http-equiv="Pragma" content="no-cache" />
                    <meta http-equiv="Expires" content="0" />
                    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
                    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
                    <meta author="山野羡民（1032694760@qq.com）" />
                    <title>%s</title>
                </head>
                <body>
                    %s
                </body>
                </html>
                """)
                .formatted(title, content);
    }

}
